import { Modal } from 'ant-design-vue';
import { defHttp } from '/@/utils/http/axios';
import { getCacheByDynKey, setCacheByDynKey } from '/@/utils/cache/index'
import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();
import { useGlobSetting } from '/@/hooks/setting';
const globSetting = useGlobSetting()

export const useGitUser = (gitUserId) => {
  
  const cacheKey = 'git_user'
  const currentUrl = window.location.protocol + "//" + window.location.host + "" + window.location.pathname;

  const getGitUserById = (id) => {
    return defHttp.get({ url: '/auth-service/gitUser/getUser', params: { id } });
  }
  
  const renderAuth = (params) => {
    return defHttp.get({ url: `/auth-service/gitUser/authorize/gitee`, params });
  }

  const showPageModal = () => {
    Modal.confirm({
      title: '访问失败',
      content: '开源不易, 麻烦各位小伙伴帮忙点点star, 即将跳转到项目页面',
      okText: '确认',
      visible: true,
      cancelButtonProps: { style: { display: 'none' } }, // 隐藏取消按钮
      onOk: () => {
        window.open('https://gitee.com/lingyang-open/lingyang-boot', '_blank')
      },
    });
  }
  
  const showModal = () => {
    Modal.confirm({
      title: '访问失败',
      content: '原因: 本项目100%开源, 是一个可商用项目, 开源不易, 麻烦各位小伙伴帮忙点点star, 点击之后重新刷新登录页面即可',
      okText: '确认',
      visible: true,
      cancelButtonProps: { style: { display: 'none' } }, // 隐藏取消按钮
      onOk: async () => {
        const respData = await renderAuth({ backUrl: currentUrl })
        if (respData.pageUrl) {
          window.location.href = respData.pageUrl
        }
      },
    });
  }

  const checkStar = async () => {
    const gitStarTipDomain = globSetting.gitStarTipDomain
    console.log('gitStarTipDomain', gitStarTipDomain);
    const domain = window.location.protocol + '//' + window.location.hostname
    console.log('current domain: ', domain);
    
    if (gitStarTipDomain !== domain) {
      return
    }
    const cacheData = getCacheByDynKey(cacheKey)
    console.log('cacheGitUser: ', cacheData);
    if (!gitUserId && !cacheData) {
      showModal()
    } else {
      const resp = await getGitUserById(gitUserId || cacheData?.id)
      console.log('getGitUser', resp);
      
      if (!resp) {
        showModal()
        return
      } 
      
      if (!resp.isStar) {
        console.log('open project page')
        showPageModal()
        return
      } 
  
      setCacheByDynKey(cacheKey, resp)
    }
    // setCacheByDynKey(cacheKey, undefined)
  }
  
  const confirmStar = async () => {
    const gitStarTipDomain = globSetting.gitStarTipDomain
    const domain = window.location.protocol + '//' + window.location.hostname
    if (gitStarTipDomain !== domain) {
      return
    }

    const cacheData = getCacheByDynKey(cacheKey)
    console.log('cacheGitUser: ', cacheData);
    if (!cacheData || !cacheData?.id) {
      showModal()
      window.location.reload()
    }
    const resp = await getGitUserById(gitUserId || cacheData?.id)
    console.log('getGitUser', resp);
    
    if (!resp || !resp.isStar) {
      window.location.reload()
    } 
    console.log('cacheGitUser: ', cacheData);
    if (!cacheData) {
      window.location.reload()
    }
  }
  return  {
    checkStar, confirmStar
  }
}